# coding=utf-8

import web

import settings
import utils

import models

render = settings.RENDER

class Index(object):
    """酒店首页"""
    def GET(self):
        ipt = web.input(mode = None, q = '')

        # 获取酒店
        result, pager = self.search_hotels()
        # 是否简洁模式
        if ipt.mode == 'lite':
            return self.render_lite(result, pager, ipt.q)
        else:
            return self.render()

    def render_lite(self, hotels, pager, word):
        """精简模式"""
        return render.admin.hotels.index_lite(hotels, pager, word)

    def render(self):
        """完整模式"""
        pass

    def search_hotels(self):
        """搜索酒店"""
        ipt = web.input(page = '0', q = '')

        # 每页记录数
        perpage = 10
        # 页码
        page = utils.intval(ipt.page)
        # 检索结果
        result = models.get_hotels(ipt.q, page * perpage, perpage)
        # 分页
        pager = utils.lite_pager(result.totalrecords, page, mode = 'lite', q = ipt.q.encode('utf-8'))

        return result, pager
